<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">

<style type="text/css">

ul.pages li{
	padding:5px;
	margin:10px 0px;
	font-size:14px;
	cursor:pointer;
	border:1px solid #ccc;
	background:#f0f0f0;
}
#snaptarget{
	background: none;
    border: 0 none;
    min-height: 100px;
    min-width: 80%;
}
ul#sortable li, .ui-state-highlight {
	margin: 10px 0;
	padding:7px;
	cursor:move;
	width:100% !important;
}
ul#sortable li > ul li{
	width:95% !important;
}
.ui-state-highlight {
	min-height:15px;
	background:#000;
	width:100%;
}
ul#sortable li a{
	float: right;
    font-weight: bold;
    text-align: right;
    color:#000;
}
ul#sortable li a:hover{
	text-decoration:underline;
}
</style>
<div class="onecolumn" >
	<div class="header"> <span ><span class="ico gray notepad"></span><?php echo $this->title; ?></span></div>
    
    <div class="clear"></div>
    
<div class="content">
	<div class="column_left">
		<div class="onecolumn">
		
			<div class="header"><span><span class="ico gray notepad"></span> All Pages</span></div>
		
			<div class="clear"></div>
		
			<div class="content">
				<?php 
				$form=$this->beginWidget('CActiveForm', array(
						'id'=>'validation',
						//'action'=>$action,
				));
				$data = CHtml::listData($pages, 'id', 'title');
					echo $form->checkBoxList($model, 'page_id', $data, array('multiple'=>true) );
				?>
				
				<div class="clear"></div>
				<br/><br/>
				<?php echo CHtml::submitButton('Add to Menu', array('class'=>'uibutton', 'id'=>'add_menu'))?>
				
				<?php $this->endWidget(); ?>
			</div>
		</div>
	</div>	
	
	<div class="column_left">
		<div class="onecolumn">
			<div class="header"><span><span class="ico gray paragraph_justify"></span>Manu Bar</span></div>
			
			<div class="clear"></div>
			
			<div class="content ui-widget-header" id="snaptarget">
				<?php if(count($menus) > 0): ?>
					<ul class="serialization vertical" id="sortable">
						<?php foreach($menus as $menu):?>
							<li class="ui-state-default" data-name="<?php echo $menu->page_id?>" data-page_id="<?php echo $menu->id?>"><?php echo ucfirst(Cms::model()->findByPk($menu->page_id)->title)?><a href="javascript:void(0);" data-value="<?php echo $menu->id;?>" class="delete-menu" alt="Delete">X</a>
								<ul>
									<?php 
										$submenus = Menuitems::model()->findAllByAttributes(array('parent'=>$menu->id), array('order'=>'sequence'));
										if ( isset($submenus) &&!empty($submenus) ):
										foreach($submenus as $submenu):
									?>
											<li class="ui-state-default" data-name="<?php echo $menu->page_id?>" data-page_id="<?php echo $submenu->id?>"><?php echo ucfirst(Cms::model()->findByPk($submenu->page_id)->title)?><a href="javascript:void(0);" data-value="<?php echo $submenu->id;?>" class="delete-menu" alt="Delete">X</a></li>
										
									<?php endforeach; endif;?>
								</ul>	
							</li>	
						<?php endforeach;?>
					</ul>
				<?php else:?>
					<p>No item(s) / page(s) in menu bar</p>
				<?php endif;?>
			</div>
	</div>
</div>
<?php Yii::app()->clientScript->registerCoreScript('jquery');?>
<script src="<?php echo Yii::app()->getBaseUrl()?>/admin_assets/js/jquery.sortable.js"></script>
<script>
		var oldContainer
		var group = $("#sortable").sortable({
		  group: 'serialized',
		  
		  onDrop: function (item, container, _super) {
		    var data = group.sortable("serialize").get();
		    var jsonString = JSON.stringify(data, null, ' ');
			$.ajax({
				data:'menuPosition=<?php echo $menuPosition;?>&data='+jsonString,
				url: '<?php echo Yii::app()->getBaseUrl()?>/admin/menu/changeOrder',
				type: 'post',
				
			});
		    //$('#serialize_output2').text(jsonString);
		    _super(item, container)
		  }
		})

    $('#add_menu').on('click', function(e){
        e.preventDefault();
        if ( $('form#validation input:checked').length > 0 )
        	$('form#validation').submit();
        else
        	showError('select atleast one page for menu',1500);
	});

	$('a.delete-menu').on('click', function(){
		$this = $(this);
		req = $this.data('value');
		$.ajax({
			url: '<?php echo Yii::app()->getBaseUrl()?>/admin/menu/delete',
			data: 'id='+req,
			type:'post',
			success: function(resp){
				if (resp == 'success' )
					$this.parent().fadeOut();
			},
		}); 
	});
</script>






